<?php

namespace fztadmin\modules\ajax\controllers;

use common\queues\LogFengZhangTongSend;
use common\Tools;
use fztadmin\modules\user\models\forms\Custom;
use yii\web\Controller;
use yii\web\Response;

class PlatController extends Controller
{
    /**
     * 平台资金查询
     */
    public function actionMoneyInquiry()
    {
        $fzt = \Yii::$app->fenZhangTong;
        $acctTypeArr = $fzt->getPlatUseIESI111018AcctTypeArr();
        $inpuiries = $this->IESI111018($acctTypeArr);
        return $this->renderAjax('_show-money-inquiry', ['inpuiries' => $inpuiries]);
    }

    /**
     * 客户资金查询
     */
    public function actionCustomMoneyInquiry($custom_id)
    {
        $custom = Custom::findOne($custom_id);
        if (!$custom) {
            throw new \Exception("未找到客户");
        }
        if (!$custom->plat_cust_no) {
            return "客户编号丢失了，请联系工作人员";
        }
        $fzt = \Yii::$app->fenZhangTong;
        $acctTypeArr = $fzt->getCustomUseIESI111018AcctTypeArr();
        $inpuiries = $this->IESI111018($acctTypeArr, $custom->plat_cust_no);
        return $this->renderAjax('_show-money-inquiry', ['inpuiries' => $inpuiries]);
    }

    protected function IESI111018($acctTypeArr = [], $platCustNo = null)
    {
        $inpuiries = [];
        $fzt = \Yii::$app->fenZhangTong;
        $respUseKey = $fzt->getIESI111018RespMoneyArr();
        foreach ($acctTypeArr as $k => $v){
            $transId = uniqid();
            $params = [
                'acctType' => $v,
                'transId' => $transId,//请求业务流水号
            ];
            if ($platCustNo) {
                $params['platCustNo'] = $platCustNo;
            }
            $fzt->IESI111018($params);
            $respUseKekArrVal = [];
            foreach ($respUseKey as $k1 => $v1) {
                $respUseKekArrVal[$v1] = $fzt->resp_arr[$v1] ?? 0;
            }
            if ($fzt->ret_code != "00000") {
                foreach ($respUseKey as $k1 => $v1) {
                    $respUseKekArrVal[$v1] = "获取异常:{$fzt->ret_msg}";
                }
            }
            $inpuiry = [
                'acctType' => $v,
                'acctTypeDesc' => $fzt->getIESI111018AcctTypeDesc()[$v],
            ];
            $inpuiries[$v] = array_merge($inpuiry, $respUseKekArrVal);
        }
        return $inpuiries;
    }

    protected function IESI111107($acctType)
    {
        \Yii::$app->response->format = Response::FORMAT_JSON;
//        $transId = uniqid();
//        $params = [
//            'transId' => $transId,
//            'acctType' => $acctType,
//            'bgnDate' => "20211001",
//            'endDate' => "20211223",
//            'pageNo' => "1",
//            'pageSize' => "2",
//        ];
//
//        $fzt = \Yii::$app->fenZhangTong;
//        $fzt->IESI111107($params);
//        if ($fzt->ret_code != "00000") {
//            throw new \Exception($fzt->ret_msg);
//        }
        $json = <<<JSON
{
  "draw": 1,
  "recordsTotal": 57,
  "recordsFiltered": 57,
  "data": [
    {
      "first_name": "Airi",
      "last_name": "Satou",
      "position": "Accountant",
      "office": "Tokyo",
      "start_date": "28th Nov 08",
      "salary": "$162,700"
    },
    {
      "first_name": "Angelica",
      "last_name": "Ramos",
      "position": "Chief Executive Officer (CEO)",
      "office": "London",
      "start_date": "9th Oct 09",
      "salary": "$1,200,000"
    },
    {
      "first_name": "Ashton",
      "last_name": "Cox",
      "position": "Junior Technical Author",
      "office": "San Francisco",
      "start_date": "12th Jan 09",
      "salary": "$86,000"
    },
    {
      "first_name": "Bradley",
      "last_name": "Greer",
      "position": "Software Engineer",
      "office": "London",
      "start_date": "13th Oct 12",
      "salary": "$132,000"
    },
    {
      "first_name": "Brenden",
      "last_name": "Wagner",
      "position": "Software Engineer",
      "office": "San Francisco",
      "start_date": "7th Jun 11",
      "salary": "$206,850"
    },
    {
      "first_name": "Brielle",
      "last_name": "Williamson",
      "position": "Integration Specialist",
      "office": "New York",
      "start_date": "2nd Dec 12",
      "salary": "$372,000"
    },
    {
      "first_name": "Bruno",
      "last_name": "Nash",
      "position": "Software Engineer",
      "office": "London",
      "start_date": "3rd May 11",
      "salary": "$163,500"
    },
    {
      "first_name": "Caesar",
      "last_name": "Vance",
      "position": "Pre-Sales Support",
      "office": "New York",
      "start_date": "12th Dec 11",
      "salary": "$106,450"
    },
    {
      "first_name": "Cara",
      "last_name": "Stevens",
      "position": "Sales Assistant",
      "office": "New York",
      "start_date": "6th Dec 11",
      "salary": "$145,600"
    },
    {
      "first_name": "Cedric",
      "last_name": "Kelly",
      "position": "Senior Javascript Developer",
      "office": "Edinburgh",
      "start_date": "29th Mar 12",
      "salary": "$433,060"
    }
  ]
}
JSON;
        $data = Tools::isJson($json);
        return $data;
    }
}